import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';

import '../index.dart';

/// 按钮样式
enum TagWidgetVariant {
  primary,
  secondary,
  destructive,
  outline,
  ghost,
  link,
  icon,
}

/// 按钮
class TagWidget extends StatefulWidget {
  /// 按钮样式
  final TagWidgetVariant variant;

  /// 按钮尺寸
  final WidgetScale scale;

  /// tap 事件
  final Function()? onTap;

  /// 文字字符串
  final String? text;

  /// 文字颜色
  final Color? textColor;

  /// 子组件
  final Widget? child;

  /// 图标
  final Widget? icon;

  /// 圆角
  final double? borderRadius;

  /// 背景色
  final Color? backgroundColor;

  /// 边框色
  final Color? borderColor;

  /// 宽度
  final double? _width;

  /// 高度
  final double? height;

  /// 启用
  final bool enabled;

  /// 图标和文字的间距
  final double? iconSpace;

  /// 是否loading
  final bool? loading;

  /// 是否反转
  final bool? reverse;

  /// 主轴对齐方式
  final MainAxisAlignment? mainAxisAlignment;

  /// 主轴尺寸
  final MainAxisSize? mainAxisSize;

  /// 阴影
  final double? elevation;

  const TagWidget({
    super.key,
    this.variant = TagWidgetVariant.primary,
    this.scale = WidgetScale.medium,
    this.onTap,
    this.text,
    this.borderRadius,
    this.child,
    this.backgroundColor,
    this.icon,
    this.borderColor,
    double? width,
    this.height,
    this.enabled = true,
    this.iconSpace,
    this.loading,
    this.textColor,
    this.reverse,
    this.mainAxisAlignment,
    this.mainAxisSize,
    this.elevation,
  }) : _width = width;





  @override
  State<TagWidget> createState() => _TagWidgetState();
}

class _TagWidgetState extends State<TagWidget> {
  // 按下
  bool pressed = false;

  // 按尺寸缩放
  double _size(double val) {
    switch (widget.scale) {
      case WidgetScale.medium:
        return val;
      case WidgetScale.small:
        return val * (1 - 0.3);
      case WidgetScale.large:
        return val * (1 + 0.3);
    }
  }

  // 文本样式
  Color _textColor() {
    var colorScheme = context.colors.scheme;
    Color color = colorScheme.onPrimary;
    switch (widget.variant) {
      case TagWidgetVariant.primary:
        color = widget.textColor ?? colorScheme.onPrimary;
        break;
      case TagWidgetVariant.secondary:
        color = widget.textColor ?? colorScheme.onSecondary;
        break;
      case TagWidgetVariant.destructive:
        color = widget.textColor ?? colorScheme.onError;
        break;
      case TagWidgetVariant.outline:
        color = widget.textColor ?? colorScheme.primary;
        break;
      case TagWidgetVariant.ghost:
        color = widget.textColor ?? colorScheme.onSurface;
        break;
      case TagWidgetVariant.link:
        color = widget.textColor ?? colorScheme.onSurface;
        break;
      case TagWidgetVariant.icon:
        color = widget.textColor ?? colorScheme.onPrimaryContainer;
        break;
      default:
    }

    return color;
  }

  // 背景色
  Color _backgroundColor() {
    var colorScheme = context.colors.scheme;
    Color color = colorScheme.primary;
    switch (widget.variant) {
      case TagWidgetVariant.primary:
        color = colorScheme.primary;
        break;
      case TagWidgetVariant.secondary:
        color = colorScheme.secondary;
        break;
      case TagWidgetVariant.destructive:
        color = colorScheme.error;
        break;
      case TagWidgetVariant.outline:
      case TagWidgetVariant.ghost:
        color = widget.backgroundColor ?? Colors.transparent;
      case TagWidgetVariant.link:
      case TagWidgetVariant.icon:
        color = colorScheme.surface;
        break;
      default:
    }
    return color;
  }

  // 高亮色
  Color _highlightColor() {
    var colorScheme = context.colors.scheme;
    Color color = colorScheme.primary.withOpacity(0.1);
    switch (widget.variant) {
      case TagWidgetVariant.primary:
        color = colorScheme.primaryContainer.withOpacity(0.1);
        break;
      case TagWidgetVariant.secondary:
        color = colorScheme.secondaryContainer.withOpacity(0.1);
        break;
      case TagWidgetVariant.destructive:
        color = colorScheme.errorContainer.withOpacity(0.1);
        break;
      case TagWidgetVariant.outline:
      case TagWidgetVariant.ghost:
      case TagWidgetVariant.link:
      case TagWidgetVariant.icon:
        color = colorScheme.surfaceContainer.withOpacity(0.1);
        break;
      default:
    }
    return color;
  }

  // 圆角
  BorderRadius? _borderRadius() {
    switch (widget.variant) {
      case TagWidgetVariant.primary:
      case TagWidgetVariant.secondary:
      case TagWidgetVariant.destructive:
      case TagWidgetVariant.ghost:
      case TagWidgetVariant.link:
      case TagWidgetVariant.icon:
        return null;
      case TagWidgetVariant.outline:
        return BorderRadius.circular(
            widget.borderRadius ?? _size(AppRadius.button));
      default:
    }
    return null;
  }

  BoxBorder? _border() {
    switch (widget.variant) {
      case TagWidgetVariant.primary:
      case TagWidgetVariant.secondary:
      case TagWidgetVariant.destructive:
      case TagWidgetVariant.ghost:
      case TagWidgetVariant.link:
      case TagWidgetVariant.icon:
        return null;
      case TagWidgetVariant.outline:
        return Border.all(
          color: widget.borderColor ?? context.colors.scheme.outline,
          width: AppBorder.button,
        );
      default:
    }
    return null;
  }

  bool _ripple(bool enbaled) {
    if (enbaled == false) {
      return false;
    }

    switch (widget.variant) {
      case TagWidgetVariant.link:
        return false;
      default:
        return true;
    }
  }

  // 主视图
  Widget _buildView() {
    
    return const Text("data");
  }

  @override
  Widget build(BuildContext context) {
    return _buildView();
  }
}
